﻿<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:s="http://schemas.microsoft.com/sharepoint/">

  <xsl:output method="html" indent="yes" />
  <xsl:variable name="RuleId" />
  <xsl:template match="/SpCopReport">

    <html>
      <head>
        <title>Analysis Report</title>
      </head>
      <style>
        #Title {font-family: Verdana; font-size: 14pt; color: black; font-weight: bold}
        .ColumnHeader {font-family: Verdana; font-size: 8pt; background-color:white; color: black}
        .CriticalError {font-family: Verdana; font-size: 8pt; color: darkred; font-weight: bold; vertical-align: middle; }
        .Error {font-family: Verdana; font-size: 8pt; color: royalblue; font-weight: bold; vertical-align: middle; }
        .CriticalWarning {font-family: Verdana; font-size: 8pt; color: darkorange; font-weight: bold; vertical-align: middle; }
        .Warning {font-family: Verdana; font-size: 8pt; color: darkgray; font-weight: bold; vertical-align: middle; }
        .Information {font-family: Verdana; font-size: 8pt; color: black; font-weight: bold; vertical-align: middle; }

        .PropertyName {font-family: Verdana; font-size: 8pt; color: black; font-weight: bold}
        .PropertyContent {font-family: Verdana; font-size: 8pt; color: black}
        .NodeIcon { font-family: WebDings; font-size: 12pt; color: navy; padding-right: 5;}
        .MessagesIcon { font-family: WebDings; font-size: 12pt; color: red;}
        .RuleDetails { padding-top: 10;}
        .SourceCode { background-color:#DDDDFF; }
        .RuleBlock { }
        .MessageNumber { font-family: Verdana; font-size: 10pt; color: darkred; }
        .MessageBlock { font-family: Verdana; font-size: 10pt; color: darkred; }
        .Resolution {font-family: Verdana; font-size: 8pt; color: black; }
        .NodeLine { font-family: Verdana; font-size: 9pt;}
        .Note { font-family: Verdana; font-size: 9pt; color:black;  }
        .NoteUser { font-family: Verdana; font-size: 9pt; font-weight: bold; }
        .NoteTime { font-family: Verdana; font-size: 8pt; font-style: italic; }
        .Button { font-family: Verdana; font-size: 9pt; color: blue; background-color: #EEEEEE; border-style: outset;}
        a:link { color: blue; text-decoration: none; }
        a:visited { color: blue; text-decoration: none; }
        a:active { color: blue; text-decoration: none; }
      </style>
      <script>
        function ViewState(blockId)
        {
        var block = document.getElementById(blockId);
        if (block.style.display=='none')
        {
        block.style.display='block';
        if (block.className == 'MessageDiv')
        {
        var toggle = document.getElementById(blockId + "Toggle");
        toggle.innerHTML = "&#x0036;";
        }
        }
        else
        {
        block.style.display='none';
        if (block.className=='MessageDiv')
        {
        var toggle = document.getElementById(blockId + "Toggle");
        toggle.innerHTML = "&#x0034;";
        }
        }
        }

        function SwitchAll(how)
        {
        var nodes = document.getElementsByTagName("div");
        for (i = 0; i != nodes.length;i++)
        {
        var block = nodes[i];
        if (block != null)
        {
        if (block.className == 'NodeDiv' || block.className == 'MessageBlockDiv' || IsMessageDivWithActionNone(block, how))
        {
        block.style.display=how;
        }
        }
        }
        }

        function IsMessageDivWithActionNone(block, how)
        {
        if (block.className != 'MessageDiv') return false;
        if (how != 'none') return false;

        //as we're collapsing the tree, set the correct toggle icon
        var toggle = document.getElementById(block.id + "Toggle");
        if(toggle != null)
        {
        toggle.innerHTML = "&#x0034;";
        }
        return true;
        }

        function ExpandAll()
        {
        SwitchAll('block');
        }

        function CollapseAll()
        {
        SwitchAll('none');
        }

        function DoNothing() {}

        function ButtonState(blockId)
        {
        var block = document.getElementById(blockId);
        if (block.style.borderStyle=='inset')
        {
        block.style.borderStyle='outset';
        }
        else
        {
        block.style.borderStyle='inset';
        }
        }
      </script>
      <body bgcolor="white" alink="Black" vlink="Black" link="Black">

        <!-- Report Title -->
        <div id="Title">
          SharePointCop
          <xsl:value-of select="@Version" />
          Analysis Report
        </div>

        <xsl:variable name="MessageCount" select="count(.//Problem)" />
        <xsl:variable name="SolutionCount" select="count(.//Package)" />


        <div class="Information">
          Total solutions analyzed :
          <xsl:value-of select="$SolutionCount" />
          Total problems detected :
          <xsl:value-of select="$MessageCount" />
        </div>

        <br />
        <xsl:apply-templates select=".//Package" />
      </body>
    </html>

  </xsl:template>
  <xsl:template match="ProblemCollection">
    <xsl:apply-templates />
  </xsl:template>
  <xsl:template match="Package">
    <xsl:variable name="solution" select="Name" />
    <xsl:variable name="messages" select="count(/SpCopReport//ProblemCollection[@Package=$solution]//Problems//Problem)" />
    <xsl:variable name="features" select="count(Features/Feature)" />
    <xsl:variable name="ctypes" select="count(Features//Feature/Elements//Element[ElementType='ContentType'])" />
    <xsl:variable name="fields" select="count(Features//Feature/Elements//Element[ElementType='Field'])" />
    <xsl:variable name="modules" select="count(Features//Feature/Elements//Element[ElementType='Module'])" />
    <xsl:variable name="cactions" select="count(Features//Feature/Elements//Element[ElementType='CustomAction'])" />
    <xsl:variable name="cactionGroups" select="count(Features//Feature/Elements//Element[ElementType='CustomActionGroup'])" />
    <xsl:variable name="wkfls" select="count(Features//Feature/Elements//Element[ElementType='Workflow'])" />
    <xsl:variable name="sitedefs" select="count(SiteDefinitions/SiteDefinition)" />
    <xsl:variable name="solId" select="generate-id()" />


    <div class="RuleBlock">

      <nobr class="NodeIcon">&#x0031;</nobr>
      Solution :
      <xsl:value-of select="$solution" />
      (
      <xsl:value-of select="$messages" />
      Violations)
      <div class="Note">
        <ul>
          <xsl:if test="$features > 0">
            <li>
              <xsl:variable name="featDivId" select="generate-id(Features//Feature)" />
              <div>
                <xsl:attribute name="onClick">
                  javascript:ViewState('
                  <xsl:value-of select="$featDivId" />
                  ');
                </xsl:attribute>
                <a href="javascript:DoNothing()">
                  <nobr class="NodeIcon">&#x0032;</nobr>
                </a>
                <xsl:value-of select="$features" />
                Features
              </div>
              <div style="display:none">
                <xsl:attribute name="Id">
                  <xsl:value-of select="$featDivId" />
                </xsl:attribute>
                <xsl:apply-templates select="Features//Feature" />
              </div>
            </li>
          </xsl:if>
          <xsl:if test="$ctypes > 0">
            <li>
              <xsl:variable name="cTypesDivId" select="generate-id(Features//Feature/Elements//Element[ElementType='ContentType'])" />
              <div>
                <xsl:attribute name="onClick">
                  javascript:ViewState('
                  <xsl:value-of select="$cTypesDivId" />
                  ');
                </xsl:attribute>
                <a href="javascript:DoNothing()">
                  <nobr class="NodeIcon">&#x0032;</nobr>
                </a>
                <xsl:value-of select="$ctypes" />
                Content Types
              </div>
              <div style="display:none">
                <xsl:attribute name="Id">
                  <xsl:value-of select="$cTypesDivId" />
                </xsl:attribute>
                <xsl:apply-templates select="Features//Feature/Elements//Element[ElementType='ContentType']" mode="cType" />
              </div>
            </li>
          </xsl:if>
          <xsl:if test="$fields > 0">
            <li>
              <xsl:variable name="fieldsDivId" select="generate-id(Features//Feature/Elements//Element[ElementType='Field'])" />
              <div>
                <xsl:attribute name="onClick">
                  javascript:ViewState('
                  <xsl:value-of select="$fieldsDivId" />
                  ');
                </xsl:attribute>
                <a href="javascript:DoNothing()">
                  <nobr class="NodeIcon">&#x0032;</nobr>
                </a>
                <xsl:value-of select="$fields" />
                Fields
              </div>
              <div style="display:none">
                <xsl:attribute name="Id">
                  <xsl:value-of select="$fieldsDivId" />
                </xsl:attribute>
                <xsl:apply-templates select="Features//Feature/Elements//Element[ElementType='Field']" mode="field" />
              </div>
            </li>
          </xsl:if>
          <xsl:if test="$modules > 0">
            <li>
              <xsl:variable name="modsDivId" select="generate-id(Features//Feature/Elements//Element[ElementType='Module'])" />

              <div>
                <xsl:attribute name="onClick">
                  javascript:ViewState('
                  <xsl:value-of select="$modsDivId" />
                  ');
                </xsl:attribute>
                <a href="javascript:DoNothing()">
                  <nobr class="NodeIcon">&#x0032;</nobr>
                </a>
                <xsl:value-of select="$modules" />
                Modules
              </div>
              <div style="display:none">
                <xsl:attribute name="Id">
                  <xsl:value-of select="$modsDivId" />
                </xsl:attribute>
                <xsl:apply-templates select="Features//Feature/Elements//Element[ElementType='Module']" mode="module" />
              </div>
            </li>
          </xsl:if>
          <xsl:if test="$cactions > 0">
            <li>
              <xsl:value-of select="$cactions" />
              CustomActions
            </li>
          </xsl:if>
          <xsl:if test="$cactionGroups > 0">
            <li>
              <xsl:value-of select="$cactionGroups" />
              CustomActionGroups
            </li>
          </xsl:if>
          <xsl:if test="$wkfls > 0">
            <li>
              <xsl:value-of select="$wkfls" />
              Workflows
            </li>
          </xsl:if>
          <xsl:if test="$sitedefs > 0">
            <li>
              <xsl:value-of select="$sitedefs" />
              Site Definition
            </li>
          </xsl:if>

        </ul>
      </div>

      <xsl:if test="$messages > 0">
        <xsl:apply-templates select="/SpCopReport//ProblemCollection[@Package=$solution]//Problems" />


      </xsl:if>
      <xsl:if test="$messages = 0">
        <div class="Note">
          No Violations :0) Good Job !
        </div>
      </xsl:if>
    </div>
    <hr class="NodeLine" />
  </xsl:template>
  <xsl:template match="Problems">
    <xsl:apply-templates select="Rule" />
    <xsl:apply-templates select="Problem" />
    <br />
  </xsl:template>
  <xsl:template match="Problems">
    <xsl:variable name="MessageBlockId" select="generate-id()" />
    <div class="MessageBlock">
      <xsl:attribute name="onClick">
        javascript:ViewState('
        <xsl:value-of select="$MessageBlockId" />
        ');
      </xsl:attribute>
      <a href="javascript:DoNothing()">
        <nobr class="MessagesIcon">&#x005D;</nobr>
      </a>
      <xsl:variable name="MessageCount" select="count(Problem)" />
      <xsl:value-of select="$MessageCount" />
      Message
      <xsl:choose>
        <xsl:when test="$MessageCount > 1">s</xsl:when>
      </xsl:choose>
      for
      <xsl:value-of select="../Rule/CheckId" />
      :
      <xsl:value-of select="../Rule/Name" />
    </div>
    <div class="MessageBlockDiv" style="display: none; position: relative; padding-left: 5;">
      <xsl:attribute name="id">
        <xsl:value-of select="$MessageBlockId" />
      </xsl:attribute>
      <div class="RuleDetails">
        <xsl:value-of select="../Rule/LongDescription"></xsl:value-of>
      </div>
      <table width="100%">
        <tr>
          <td class="ColumnHeader"></td>
          <td class="ColumnHeader">Message Level</td>
          <td class="ColumnHeader">Certainty</td>
          <td class="ColumnHeader" width="100%">Resolution</td>
        </tr>
        <xsl:apply-templates select="Problem" />
      </table>
    </div>

  </xsl:template>
  <xsl:template match="Problem">
    <xsl:variable name="messageId" select="generate-id()" />
    <xsl:variable name="rulename" select="@TypeName" />

    <tr>
      <td></td>
      <td colspan="3">
        <div class="MessageDiv" style="display: none">
          <xsl:attribute name="id">
            <xsl:value-of select="$messageId" />
          </xsl:attribute>

          <!--- Rule Details  -->
          <table width="100%" class="MessageBlock">
            <xsl:apply-templates select="/Description" />
            <xsl:apply-templates select=".//Problems/Rule[@TypeName=$rulename]" />
          </table>
        </div>
      </td>
    </tr>
    <tr>
      <td>
        <nobr class="NodeIcon">&#x003D;</nobr>
      </td>
      <td>
        <xsl:value-of select="../../Rule/MessageLevel/Value" />
      </td>
      <td>
        <xsl:value-of select="../../Rule/MessageLevel/Certainty" />
      </td>
      <td>
        <div class="Resolution">
          <!--<nobr class="NodeIcon">&#x00EB;</nobr>
        <nobr class="NodeIcon">&#x007E;</nobr>
        <nobr class="NodeIcon">&#x0098;</nobr>-->
          <xsl:value-of select="Description" />
        </div>
      </td>
    </tr>
  </xsl:template>
  <xsl:template match="Rule">
    <tr>
      <td colspan="4" class="RuleDetails">
        <xsl:value-of select="Description" />
      </td>
    </tr>
  </xsl:template>
  <xsl:template match="Feature">
    <xsl:variable name="visible" select="Xml/s:Feature/@Hidden" />
    <xsl:variable name="scope" select="Xml/s:Feature/@Scope" />
    <nobr class="NodeIcon">&#x0034;</nobr>
    <xsl:value-of select="Folder" />
    <xsl:if test="$visible='TRUE'">( Hidden )</xsl:if>
    - Scope :
    <xsl:value-of select="$scope" />
    <br />
  </xsl:template>
  <xsl:template match="Element" mode="cType">
    <nobr class="NodeIcon">&#x0034;</nobr>
    <xsl:value-of select="Xml/s:ContentType/@Name" />
    -
    <xsl:value-of select="Xml/s:ContentType/@Group" />
    <br />
  </xsl:template>
  <xsl:template match="Element" mode="field">
    <nobr class="NodeIcon">&#x0036;</nobr>
    <div>
      <xsl:value-of select="Xml/s:Field/@Name" />
      -
      <xsl:value-of select="Xml/s:Field/@Type" />
      <small>
        <xsl:if test="Xml/s:Field/@Group">Hidden</xsl:if>
      </small>
    </div>
    <br />
  </xsl:template>
  <xsl:template match="Element" mode="module">
    <xsl:variable name="filesDivId" select="generate-id(Xml/s:Module)" />
    <div class="MessageBlock">
      <xsl:attribute name="onClick">
        javascript:ViewState('
        <xsl:value-of select="$filesDivId" />
        ');
      </xsl:attribute>
      <a href="javascript:DoNothing()">
        <nobr class="NodeIcon">&#x0036;</nobr>
      </a>
      <xsl:value-of select="Xml/s:Module/@Name" />
      -
      <xsl:value-of select="Xml/s:Module/@Url" />
      (
      <xsl:value-of select="count(Xml/s:Module/s:File)" />
      Files )
    </div>
    <div style="display:none">
      <xsl:attribute name="id">
        <xsl:value-of select="$filesDivId" />
      </xsl:attribute>
      <ul>
        <xsl:apply-templates select="Xml/s:Module/s:File" />
      </ul>
    </div>
  </xsl:template>
  <xsl:template match="s:File">
    <li>
      <xsl:apply-templates select="@Url" />
    </li>
  </xsl:template>
</xsl:stylesheet>